<?php

/**
* baseDao.php
* 基础Dao层
* @author Yanling
*/

class baseDao extends Dao {
	/**
	 * @var对应的表明
	 */
	protected $table_name;
	protected $table_key  = 'id';

	/**
	 *@var字段列表
	 */
	protected $fields ;

	/**
	 *	文件字段
	 *	@var [array]
	 */
	protected $filefields = array();

	/**
	 * 	添加一条数据
	 *	@param $model [数据模型数据] array('key1'=>'val1','key2'=>'val2')
	 *	@return $id 
	 */
	public function create($model){
		$model = $this->dao->db->build_key($model,$this->fields);
		return $this->dao->db->insert($model,$this->table_name);
	}

	/**
	 * 	添加多条数据
	 *	@param $fields [字段数组] array('field1','field2',...)
	 *	@param $model [值数组]	array(array('val1','val2'),array('val1','val2'),...)
	 *	@return $ids 
	 */
	public function createMore($fields,$model){
		return $this->dao->db->insert_more($fields,$model,$this->table_name);
	}

	/**
	 *	更新 [根据主键id更新数据]
	 *	@param $model [数据模型数据] array('id'=>'','key1'=>'val1','key2'=>'val2')
	 * 	@return bool
	 */
	public function update($model){
		$id = $model[$this->table_key];
		unset($model[$this->table_key]);
		$model = $this->dao->db->build_key($model,$this->fields);
		return $this->dao->db->update($id, $model,$this->table_name);
	}


	/**
	 * 	更新 [根据字段更新数据]
	 *	@param $model [数据模型数据] array('key1'=>'val1','key2'=>'val2')
	 * 	@return bool
	 */
	public function updateByFields($model,$fields){
		unset($model[$this->table_key]);
		$model = $this->dao->db->build_key($model,$this->fields);
		return $this->dao->db->update_by_field($model, $fields, $this->table_name);
	}

	/**
	 * 	删除 [根据ID值删除表数据,可以删除单个或者多个数据]
	 * 	@param  $ids [id数据] array('val1','val2',...)
	 * 	@return bool
	 */
	public function delete($ids,$del_file = true){
		if($del_file = true){
			$this->deleteFiles($ids);
		}
	 	return $this->dao->db->delete($ids, $this->table_name, $this->table_key);
	}

	/**
	 *  [deleteFiles 数据库中存储的文件路径所对应]
	 */
	protected function deleteFiles($ids){
		if(empty($this->filefields)){
			return ;
		}
		$fileupload= InitPHP::getLibrary('fileupload'); //文件上传类加载
		$deletepaths = array();
		foreach ($ids as $id) {
			$row = $this->findOneById($id);
			foreach ($this->filefields as $field) {
				$deletepaths[] =  WWW_PATH . $row[$field];
			}
		}
		$fileupload->del($deletepaths);
	}

	/**
	 * 	[count 查询数据条数]
	 * 	@param  $fields [查询条件数组] array('key1'=>'val1','key2'=>'val2')
	 * 	@return int 
	 */
	public function count($fields){
	 	return $this->dao->db->get_count($this->table_name,$fields);
	}

	/**
	 * 	通过id查找映射
	 * 	@param $id
	 * 	@return array
	 */
	public function findOneById($id){
		return $this->dao->db->get_one($id,$this->table_name);
	}

	/**
	 * 	通过条件查找一个映射
	 * 	@param $fields  array('key1'=>'val1','key2'=>'val2');
	 * 	@return array
	 */
	public function findOneByFields($fields){
		return $this->dao->db->get_one_by_field($fields,$this->table_name);
	}

	/**
	 *	查找列表
	 *	@param $fields 条件数组支持复杂类型
	 *	@param $page 
	 *	@param $pagesize
	 *	@param $selectfields 要查询的字段
	 *	@param $orderkey
	 *	@param $sort
	 * 	@return array
	 */
	public function findList($fields,$page='',$pagesize='',$selectfields='',$orderkey='',$sort='DESC'){
		$where = $this->dao->db->build_where($fields);

		if(is_array($selectfields)){
			$selectfields = $this->dao->db->build_implode($selectfields,1);
		}else{
			$selectfields = '*';
		}
		
		$orderkey = ($orderkey == '') ? $this->table_key : $orderkey;	
		if($page == ''&&$pagesize == ''){
			$sql = sprintf("SELECT %s FROM %s %s ORDER BY %s %s",$selectfields,$this->table_name,$where,$orderkey,$sort);
		}else{
			$start = ($page-1)*$pagesize;
			$limit = $this->dao->db->build_limit($start,$pagesize);
			$sql = sprintf("SELECT %s FROM %s %s ORDER BY %s %s %s",$selectfields,$this->table_name,$where,$orderkey,$sort,$limit);
		}

		return $this->dao->db->get_all_sql($sql);
	}

	/**
	 *	查找个数
	 *	@param $fields 条件数组支持复杂类型
	 * 	@return int
	 */
	public function findListNum($fields){
		return $this->dao->db->get_count($this->table_name,$fields);
	}

	/**
	 *	查找键值对
	 *	@param $k_filed 键key
	 *	@param $v_filed 值key
	 *	@param $field 条件数组支持复杂类型
	 *	@param $page
	 *	@param $pagesize
	 *	@return 键值对
	 */
	public function findKV($k_filed, $v_filed,$fields=array(),$page='',$pagesize=''){
		$result = $this->findList($fields,$page,$pagesize); 
		$kv =array();
		foreach($result as $row){
			$kv[$row[$k_filed]] = $row[$v_filed];
		}
		return $kv;
	}


	/**
	 * [findQuery 特殊sql条件查询]
	 * @param  string $query    [特殊sql语句]
	 * @param  array $fields    [查询条件]
	 * @param  string $orderkey 
	 * @param  string $sort     
	 * @return [type]           
	 */
	public function findQuery($query,$fields,$orderkey='',$sort='DESC'){
		$where = $this->dao->db->build_where($fields);
		if(empty($orderkey)){
			$sql = $query." ".$where;
		}else{
			$sql = $query." ORDER BY ".$orderkey." ".$sort." ".$where;
		}
		return $this->dao->db->get_all_sql($sql);
	}

}